<?php

namespace common\sms\aliyun;

use Aliyun\DySDKLite\SignatureHelper;
use Cm;
use common\models\CmDataModel;
use yii\db\Exception;

include_once __DIR__ . '/SignatureHelper.php';

class Aliyun
{
    /**
     * @var AuthCode
     */
    public $authCode;

    function __construct()
    {
        $this->authCode = new AuthCode();
    }
}

class AuthCode
{
    private $sessionKey = 'sms-code';

    /**
     * @var SignatureHelper
     */
    private $helper;

    function __construct()
    {
        $this->helper = new SignatureHelper();
    }

    function send($tel,$type,$data)
    {
        $flag = false;
        try {
            $helper = $this->helper;
            $config = \Yii::$app->params['sms']['aliyun'];
            $accessKeyId = $config['id'];
            $accessKeySecret = $config['key'];
            $domain = "dysmsapi.aliyuncs.com";
            $OutId = (new CmDataModel())->number();

            $params = array();

            $params["PhoneNumbers"] = $tel;
            $params["SignName"] = $config['signname'];
            $params = $this->getTemplate($params,$type,$data);

            $params['OutId'] = $OutId;

            // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
            if (!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
                $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
            }
            $res = $helper->request($accessKeyId, $accessKeySecret, $domain,
                array_merge($params, array(
                        "RegionId" => "cn-hangzhou",
                        "Action" => "SendSms",
                        "Version" => "2017-05-25",
                    )
                ));
            $resMsg = isset($res['Message']) ? $res['Message'] : '';
            $resCode = isset($res['Code']) ? $res['Code'] : '';
            if ($resMsg == 'OK' && $resCode == 'OK') {

                
                $id = Cm::$db->insert('sms_log', [
                    "number" => $OutId,
                    "type" => $type,
                    "tel" => $tel,
                    "req_data" => json_encode($params),
                    "res_data" => json_encode($res),
                    "addtime" => date('Y-m-d H:i:s',time())
                ]);

                $flag = true;
            }

        } catch (\yii\console\Exception $e) {

        }

        return $flag;
    }

    function check($tel, $code)
    {
        $data = \Yii::$app->session->get($this->sessionKey);
        $_tel = isset($data['tel']) ? $data['tel'] : '';
        $_code = isset($data['code']) ? $data['code'] : '';
        if ($tel == $_tel && $_code == $code)
            return true;
        return false;
    }

    private function getTemplate($params,$type,$data){
        if($type == 'change'){
            $params["TemplateCode"] = "SMS_200700888";  
        }elseif ($type == 'order') {
            $params["TemplateCode"] = "SMS_200710825"; 
        }elseif ($type=='refund') {
            $params["TemplateCode"] = "SMS_200710842"; 
        }elseif ($type=='sendcar') {
            $params["TemplateCode"] = "SMS_200720761";
        }elseif ($type=='carpool') {
            $params["TemplateCode"] = "SMS_200720761";
        }elseif ($type=='notice') {
            $params["TemplateCode"] = "SMS_200700908";
        }
        $params['TemplateParam'] = $data; 
        return $params;
    }
}